package com.piggy.logi.sys.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.piggy.common.satoken.utils.SecurityUtils;
import com.piggy.logi.sys.bo.LogiCarrierCarDriverEditBo;
import com.piggy.logi.sys.bo.LogiCarrierDriverQueryBo;
import com.piggy.logi.sys.domain.LogiCarrierCarDriver;
import com.piggy.logi.sys.mapper.LogiCarrierCarDriverMapper;
import com.piggy.logi.sys.service.ILogiCarrierCarDriverService;
import com.piggy.logi.sys.service.ILogiCarrierDriverService;
import com.piggy.logi.sys.vo.LogiCarrierCarDriverExtVo;
import com.piggy.logi.sys.vo.LogiCarrierDriverExtVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 承运商车辆司机Service业务层处理
 */
@Slf4j
@Service
public class LogiCarrierCarDriverServiceImpl extends ServiceImpl<LogiCarrierCarDriverMapper, LogiCarrierCarDriver> implements ILogiCarrierCarDriverService {

    @Resource
    private ILogiCarrierDriverService logiCarrierDriverService;

    @Override
    public Map<Long, List<LogiCarrierCarDriverExtVo>> getCarDrivers(List<Long> carIds) {
        if (CollUtil.isEmpty(carIds)) {
            return Maps.newHashMap();
        }
        // 获取关联
        List<LogiCarrierCarDriverExtVo> carrierCarDriverList = BeanUtil.copyToList(baseMapper.selectByCarIds(carIds),LogiCarrierCarDriverExtVo.class);
        Map<Long, List<LogiCarrierCarDriverExtVo>> carrierCarDriverMap = carrierCarDriverList.stream().collect(Collectors.groupingBy(LogiCarrierCarDriverExtVo::getCarId));

        // 获取司机信息
        List<Long> driverIds = carrierCarDriverList.stream().map(LogiCarrierCarDriverExtVo::getDriverId).distinct().collect(Collectors.toList());
        LogiCarrierDriverQueryBo bo = new LogiCarrierDriverQueryBo();
        bo.setDriverIds(driverIds);
        bo.setDeptId(SecurityUtils.getDeptId());
        List<LogiCarrierDriverExtVo> driverExtVoList = logiCarrierDriverService.queryList(bo);
        Map<Long, LogiCarrierDriverExtVo> driverExtVoMap = driverExtVoList.stream().collect(Collectors.toMap(LogiCarrierDriverExtVo::getDriverId, Function.identity()));

        Map<Long, List<LogiCarrierCarDriverExtVo>> map = Maps.newHashMap();
        carrierCarDriverList.forEach(originCarDriver -> {
            List<LogiCarrierCarDriverExtVo> carDriverExtVos = carrierCarDriverMap.get(originCarDriver.getCarId());
            if (CollUtil.isNotEmpty(carDriverExtVos)) {
                carDriverExtVos.forEach(carDriver -> {
                    LogiCarrierDriverExtVo driverExtVo = driverExtVoMap.get(carDriver.getDriverId());
                    if (driverExtVo != null) {
                        carDriver.setCarrierDriver(driverExtVo);
                    }
                });
            }
            map.put(originCarDriver.getCarId(), carDriverExtVos);
        });

        return map;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateCarDriver(Long carId, List<LogiCarrierCarDriverEditBo> driverList) {
        if (CollUtil.isEmpty(driverList)) {
            baseMapper.removeByCarId(carId);
            return;
        }
        List<LogiCarrierCarDriver> carDrivers = baseMapper.selectByCarIds(Lists.newArrayList(carId));

        List<LogiCarrierCarDriver> updates = BeanUtil.copyToList(driverList.stream().filter(item -> item.getId() != null)
                .collect(Collectors.toList()), LogiCarrierCarDriver.class);
        Set<Long> updateIds = updates.stream().map(LogiCarrierCarDriver::getId).collect(Collectors.toSet());
        List<LogiCarrierCarDriver> adds = BeanUtil.copyToList(driverList.stream().filter(item -> item.getId() == null)
                .collect(Collectors.toList()), LogiCarrierCarDriver.class);
        List<Long> removeIds = carDrivers.stream().map(LogiCarrierCarDriver::getId)
                .filter(id -> !updateIds.contains(id)).distinct().collect(Collectors.toList());

        this.updateBatchById(updates);
        this.saveBatch(adds);
        this.removeBatchByIds(removeIds);
    }
}
